package com.zhai.domain.activity.service.quota;

import com.alibaba.fastjson.JSON;
import com.zhai.domain.activity.model.aggregate.CreateQuotaOrderAggregate;
import com.zhai.domain.activity.model.entity.*;
import com.zhai.domain.activity.repository.IActivityRepository;
import com.zhai.domain.activity.service.IRaffleActivityAccountQuotaService;
import com.zhai.domain.activity.service.quota.rule.IActionChain;
import com.zhai.domain.activity.service.quota.rule.factory.DefaultActivityChainFactory;
import com.zhai.types.enums.ResponseCode;
import com.zhai.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @Description: 抽奖活动抽象类
 * @ClassName: AbstractRaffleActivity
 * @Author: zhaiyongxin
 * @Date: 2025/2/19 17:08
 * @Version: 1.0
 */
@Slf4j
public abstract class AbstractRaffleActivityAccountQuota extends RaffleActivityAccountQuotaSupport implements IRaffleActivityAccountQuotaService {

    public AbstractRaffleActivityAccountQuota(IActivityRepository activityRepository, DefaultActivityChainFactory defaultActivityChainFactory) {
        super(activityRepository, defaultActivityChainFactory);
    }

    /**
     * 以sku创建抽奖活动订单，获得参与抽奖资格（可消耗的次数）
     *
     * @param skuRechargeEntity 活动sku实体，通过sku领取活动
     * @return 活动参与记录实体
     */
    @Override
    public String createOrder(SkuRechargeEntity skuRechargeEntity) {
        log.info("创建抽奖活动单开始 userId:{} sku:{} outBusinessNo:{}", skuRechargeEntity.getUserId(), skuRechargeEntity.getSku(), skuRechargeEntity.getOutBusinessNo());
        //业务参数校验
        Long sku = skuRechargeEntity.getSku();
        String userId = skuRechargeEntity.getUserId();
        String outBusinessNo = skuRechargeEntity.getOutBusinessNo();
        if (null == sku || StringUtils.isBlank(userId) || StringUtils.isBlank(outBusinessNo)) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        // 1.根据商品sku查询sku活动信息
        ActivitySkuEntity activitySkuEntity = queryActivitySku(skuRechargeEntity.getSku());
        // 2.根据sku活动信息查询具体活动
        ActivityEntity activityEntity = queryRaffleActivityByActivityId(activitySkuEntity.getActivityId());
        // 3.根据sku活动信息查询活动数量
        ActivityCountEntity activityCountEntity = queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());

        log.info("查询结果：{} {} {}", JSON.toJSONString(activitySkuEntity), JSON.toJSONString(activityEntity), JSON.toJSONString(activityCountEntity));

        // 4.责任链处理
        IActionChain actionChain = defaultActivityChainFactory.openActionChain();
        Boolean success = actionChain.action(activitySkuEntity, activityEntity, activityCountEntity);

        // 5.构建聚合对象
        CreateQuotaOrderAggregate createQuotaOrderAggregate = buildOrderAggregate(skuRechargeEntity, activitySkuEntity, activityEntity, activityCountEntity);

        // 6.创建活动订单表
        doSaveOrder(createQuotaOrderAggregate);
        log.info("创建抽奖活动单完成 userId:{} orderId:{}", skuRechargeEntity.getUserId(), createQuotaOrderAggregate.getActivityOrderEntity().getOrderId());
        return createQuotaOrderAggregate.getActivityOrderEntity().getOrderId();
    }

    /**
     * 保存订单
     * @param createQuotaOrderAggregate
     */
    protected abstract void doSaveOrder(CreateQuotaOrderAggregate createQuotaOrderAggregate);

    /**
     * 构建下单聚合对象
     * @param skuRechargeEntity
     * @param activitySkuEntity
     * @param activityEntity
     * @param activityCountEntity
     * @return
     */
    protected abstract CreateQuotaOrderAggregate buildOrderAggregate(SkuRechargeEntity skuRechargeEntity, ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity);
}
